package com.hyt.it.ogt.ks.config;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

/**
 * @author wuyingge
 * @ClassName CacheManagerConfig.java
 * @Description encache配置
 * @createTime 2021年10月16日 17:38:00
 */
@Configuration
public class CacheManagerConfig {

    @Resource
    private ConfigManager configManager;

    @Bean
    public CacheManager cacheManager(){
        return CacheManager.getInstance();
    }

    //试卷
    @Bean(name = "paper_cache")
    public Cache getPaperCache(){
        Cache cache = cacheManager().getCache("paper_cache");
        net.sf.ehcache.config.CacheConfiguration configuration = cache.getCacheConfiguration();
        configuration.setTimeToLiveSeconds(18000);
        configuration.setTimeToIdleSeconds(36000);
        configuration.setEternal(Boolean.FALSE);
        configuration.setMaxElementsInMemory(20000);
        configuration.setMemoryStoreEvictionPolicy("LRU");
        return cache;
    }

    //试卷翻译
    @Bean(name = "paper_translate_cache")
    public Cache getPaperItemCache(){
        cacheManager().addCache("paper_translate_cache");//名称 类似于redis key
        Cache cache = cacheManager().getCache("paper_translate_cache");
        net.sf.ehcache.config.CacheConfiguration configuration = cache.getCacheConfiguration();
        configuration.setTimeToLiveSeconds(7200);//2小时
        configuration.setTimeToIdleSeconds(18000);//5小时
        configuration.setEternal(Boolean.FALSE);
        configuration.setMaxElementsInMemory(20000);
        configuration.setMemoryStoreEvictionPolicy("LRU");
        return cache;
    }

    //考试配置信息
    @Bean(name = "exam_info_cache")
    @DependsOn(value = "configManager")
    public Cache getExamInfoCache(){
        cacheManager().addCache("exam_info_cache");//名称 类似于redis key
        Cache cache = cacheManager().getCache("exam_info_cache");
        net.sf.ehcache.config.CacheConfiguration configuration = cache.getCacheConfiguration();
        configuration.setTimeToLiveSeconds(configManager.getExamCacheExpire());//10秒
        configuration.setTimeToIdleSeconds(configManager.getExamCacheExpire());//10秒
        configuration.setEternal(Boolean.FALSE);
        configuration.setMaxElementsInMemory(20000);
        configuration.setMemoryStoreEvictionPolicy("LRU");
        return cache;
    }
}
